home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libimage / rdwr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.6 KB  |  118 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    img_seek, img_write, img_read, img_optseek -
  19.  *
  20.  *                Paul Haeberli - 1984
  21.  *
  22.  */
  23. #include    <stdio.h>
  24. #include    "image.h"
  25.  
  26. img_seek(image, y, z)
  27. IMAGE         *image;
  28. unsigned int     y, z;
  29. {
  30.     if(img_badrow(image,y,z)) {
  31.     i_errhdlr("img_seek: row number out of range\n");
  32.     return EOF;
  33.     }
  34.     image->x = 0;
  35.     image->y = y;
  36.     image->z = z;
  37.     if(ISVERBATIM(image->type)) {
  38.     switch(image->dim) {
  39.         case 1:
  40.         return img_optseek(image, 512L);
  41.         case 2: 
  42.         return img_optseek(image,512L+(y*image->xsize)*BPP(image->type));
  43.         case 3: 
  44.         return img_optseek(image,
  45.             512L+(y*image->xsize+z*image->xsize*image->ysize)*
  46.                             BPP(image->type));
  47.         default:
  48.         i_errhdlr("img_seek: weird dim\n");
  49.         break;
  50.     }
  51.     } else if(ISRLE(image->type)) {
  52.     switch(image->dim) {
  53.         case 1:
  54.         return img_optseek(image, image->rowstart[0]);
  55.         case 2: 
  56.         return img_optseek(image, image->rowstart[y]);
  57.         case 3: 
  58.         return img_optseek(image, image->rowstart[y+z*image->ysize]);
  59.         default:
  60.         i_errhdlr("img_seek: weird dim\n");
  61.         break;
  62.     }
  63.     } else 
  64.     i_errhdlr("img_seek: weird image type\n");
  65. }
  66.  
  67. img_badrow(image,y,z)
  68. IMAGE *image;
  69. int y, z;
  70. {
  71.     if(y>=image->ysize || z>=image->zsize)
  72.     return 1;
  73.     else
  74.         return 0;
  75. }
  76.  
  77. img_write(image,buffer,count)
  78. IMAGE *image;
  79. char *buffer;
  80. long count;
  81. {
  82.     long retval;
  83.  
  84.     retval =  write(image->file,buffer,count);
  85.     if(retval == count) 
  86.     image->offset += count;
  87.     else
  88.     image->offset = -1;
  89.     return retval;
  90. }
  91.  
  92. img_read(image,buffer,count)
  93. IMAGE *image;
  94. char *buffer;
  95. long count;
  96. {
  97.     long retval;
  98.  
  99.     retval =  read(image->file,buffer,count);
  100.     if(retval == count) 
  101.     image->offset += count;
  102.     else
  103.     image->offset = -1;
  104.     return retval;
  105. }
  106.  
  107. img_optseek(image,offset)
  108. IMAGE *image;
  109. unsigned long     offset;
  110. {
  111.     if(image->offset != offset) {
  112.        image->offset = offset;
  113.        return lseek(image->file,offset,0);
  114.    }
  115.    return offset;
  116. }
  117.  
  118.